import api from '../../../services/api';

export default {
  /**
   * 处理视频混搭
   * @param {Array} promptData 混搭提示数据
   * @param {string} outputFolder 输出文件夹
   * @param {Object} settings 混搭设置
   * @param {Object} dateRange 日期范围
   */
  processMerge(promptData, outputFolder, settings, dateRange) {
    return api.post('/merge/process', {
      prompt_data: promptData,
      output_folder: outputFolder,
      ...settings,
      ...dateRange
    });
  },

  processMergeByIds(promptData, outputFolder, settings, dateRange) {
    return api.post('/merge/process_by_ids', {
      prompt_data: promptData,
      output_folder: outputFolder,
      ...settings,
      ...dateRange
    });
  },

  /**
   * 获取混搭任务状态
   */
  getStatus() {
    return api.get('/merge/status');
  },

  /**
   * 停止混搭任务
   */
  stopProcessing() {
    return api.post('/merge/stop');
  },

  /**
   * 更新视频搜索数据
   */
  updateSearchData() {
    return api.post('/search/update-index');
  },

  /**
   * 获取所有分类
   */
  getCategories() {
    return api.get('/search/categories');
  },

  /**
   * 视频与音频合成
   */
  mergeVideoWithAudio(videoPath, audioPath, outputPath) {
    return api.post('/merge/video-audio', {
      video_path: videoPath,
      audio_path: audioPath,
      output_path: outputPath
    });
  },

  getAudioGroup(group_id) {
    return api.get('/search/audio_group' + `?group_id=${group_id}`);
  },

  // 修改getAudioHistory方法，支持分页
  getAudioHistory(page = 1, pageSize = 10) {
    return api.get(`/search/audio_history?page=${page}&page_size=${pageSize}`);
  },

  clearLogs() {
    return api.post('/merge/clear_logs');
  },

  getVideoHistory(page = 1, pageSize = 10) {
    return api.get(`/search/video_history?page=${page}&page_size=${pageSize}`);
  },

  // 在既有导出对象中添加获取过渡特效的方法
  getTransitionEffects() {
    return api.get('/search/transition_effects');
  },

  getKeywordSuggestions(query = '', category = '') {
    return api.get(`/search/suggest_tags?query=${encodeURIComponent(query)}&category=${encodeURIComponent(category)}`);
  },

  // 添加搜索匹配视频的方法
  matchVideos(promptData, settings, dateRange) {
    return api.post('/search/matched_videos', {
      prompt_data: promptData,
      ...settings,
      ...dateRange
    });
  },

  previewCombination(params) {
    return api.post('/merge/preview_combination', {
      ...params
    });
  },

  // 添加音频上传功能
  async uploadAudio(audioFile, subtitleFile = null, options = {}) {
    const formData = new FormData();
    formData.append('audio_file', audioFile);
    
    if (subtitleFile) {
      formData.append('subtitle_file', subtitleFile);
    }
    
    if (options.groupId) {
      formData.append('group_id', options.groupId);
    }
    
    if (options.directory) {
      formData.append('directory', options.directory);
    }
    
    if (options.metaData) {
      formData.append('meta_data', JSON.stringify(options.metaData));
    }
    
    formData.append('order', options.order || 0);
    
    return api.post('/merge/audio', formData);
  },
};